//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ActivitiesSyncLibrary
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Win32;
    using System.Runtime.InteropServices;

    /// <summary>
    /// The class which interacts with the registry to add the extension.
    /// </summary>
    public class ShellExtensionHelper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ShellExtensionHelper"/> class.
        /// </summary>
        public ShellExtensionHelper() { }

        /// <summary>
        /// Associates the specified extension with progID, description, icon and application.
        /// </summary>
        /// <param name="extension">The extension.</param>
        /// <param name="progID">The prog ID.</param>
        /// <param name="description">The description.</param>
        /// <param name="icon">The icon.</param>
        /// <param name="application">The application.</param>
        public static void Associate(string extension, string progID, string description, string icon, string application)
        {
            Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);
            if (progID != null && progID.Length > 0)
            {
                // Create a key in the registry, at the classes.root level.
                using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
                {
                    // The description for this entry.
                    if (description != null)
                        key.SetValue("", description);

                    // The icon location.
                    if (icon != null)
                        key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));

                    // The application to be associated with the extension.
                    if (application != null)
                        key.CreateSubKey(@"Shell\Open\Command").SetValue("", ToShortPathName(application) + " \"%1\"");
                }
            }
        }

        /// <summary>
        /// Gets the short name of the path.
        /// </summary>
        /// <param name="lpszLongPath">The LPSZ long path.</param>
        /// <param name="lpszShortPath">The LPSZ short path.</param>
        /// <param name="cchBuffer">The CCH buffer.</param>
        /// <returns></returns>
        [DllImport("Kernel32.dll")]
        private static extern uint GetShortPathName(string lpszLongPath, [Out] StringBuilder lpszShortPath, uint cchBuffer);

        /// <summary>
        /// Return short path format of a file name.
        /// </summary>
        /// <param name="longName">The long name.</param>
        /// <returns></returns>
        private static string ToShortPathName(string longName)
        {
            StringBuilder s = new StringBuilder(1000);
            uint iSize = (uint)s.Capacity;
            uint iRet = GetShortPathName(longName, s, iSize);
            return s.ToString();
        }
    }
}
